package com.xinhe.web.session;

import com.xinhe.web.redis.ObjectRedisTemplate;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Component;

import java.util.List;
//前提是请求头要传x-access-token
@Component
public class UserSessionManager extends TokenManager{
    private static final String REDIS_KEY = "login-user";
    private static final String HEADER_WEIXIN_CONFIG_ID_KEY = "weixin_config_id";
    private static final String HEADER_ALI_CONFIG_ID_KEY = "ali_config_id";
    private static final String HEADER_ALI_APPLET_CONFIG_ID_KEY = "ali_applet_config_id";
    public static final String PLATFORM_HEADER = "platform-user-id";
    @Autowired
    private ObjectRedisTemplate<String, UserSessionView> redisTemplate;

    public Long getUserId() {
        UserSessionView view = getUserSession();
        if (null == view) {
            return null;
        }
        return view.getUserId();
    }

    public Long getPlatformUserId() {
        String idStr = getHeader("platform-user-id");
        if (StringUtils.isBlank(idStr)) {
            return null;
        }
        return Long.valueOf(Long.parseLong(idStr));
    }

    public Long getWeixinConfigId() {
        String idStr = getHeader("weixin_config_id");
        if (StringUtils.isBlank(idStr)) {
            return null;
        }
        return Long.valueOf(Long.parseLong(idStr));
    }

    public Long getAliConfigId() {
        String idStr = getHeader("ali_config_id");
        if (StringUtils.isBlank(idStr)) {
            return null;
        }
        return Long.valueOf(Long.parseLong(idStr));
    }

    public Long getAliAppletConfigId() {
        String idStr = getHeader("ali_applet_config_id");
        if (StringUtils.isBlank(idStr)) {
            return null;
        }
        return Long.valueOf(Long.parseLong(idStr));
    }

    public UserSessionView putLoginUser(Long userId, Long roleId) {
        return saveUserSession(new UserSessionView(userId, roleId));
    }

    public UserSessionView saveUserSession(UserSessionView session) {
        TokenView tokenView = saveToken(session.getUserId());
        HashOperations<String, String, UserSessionView> opsForHash = this.redisTemplate.opsForHash();
        session.setToken(tokenView.getToken());
        session.setKey(tokenView.getKey());
        opsForHash.put("login-user", session.getKey(), session);
        return session;
    }

    public UserSessionView getUserSession() {
        try {
            TokenView tokenView = getTokenView();
            if (null == tokenView) {
                return null;
            }
            HashOperations<String, String, UserSessionView> opsForHash = this.redisTemplate.opsForHash();
            return (UserSessionView)opsForHash.get("login-user", tokenView.getKey());
        } catch (Exception e) {
            return null;
        }
    }

    public boolean validRole(Long roleId) {
        UserSessionView view = getUserSession();
        if (null == view) {
            return false;
        }
        List<UserSessionView.RoleView> roles = view.getRoles();
        if (CollectionUtils.isEmpty(roles)) {
            return false;
        }
        for (UserSessionView.RoleView role : roles) {
            if (role.getId().equals(roleId)) {
                return true;
            }
        }
        return false;
    }

    public String putExtendValue(String key, String value) {
        UserSessionView session = getUserSession();
        if (null == session) {
            return null;
        }
        String result = session.putExtendValue(key, value);
        HashOperations<String, String, UserSessionView> opsForHash = this.redisTemplate.opsForHash();
        opsForHash.put("login-user", session.getKey(), session);
        return result;
    }

    public String getExtendValue(String key, String defaultValue) {
        UserSessionView view = getUserSession();
        if (null == view) {
            return null;
        }
        return view.getExtendValue(key, defaultValue);
    }

    public String getExtendValue(String key) {
        return getExtendValue(key, (String)null);
    }

    public Long getExtendValueLong(String key, Long defaultValue) {
        String value = getExtendValue(key);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return Long.valueOf(Long.parseLong(value));
    }

    public Long getExtendValueLong(String key) {
        return getExtendValueLong(key, (Long)null);
    }

    public boolean validMenu(String url) {
        List<UserSessionView.MenuView> menus = getUserSession().getMenus();
        if (CollectionUtils.isEmpty(menus)) {
            return true;
        }
        for (UserSessionView.MenuView menu : menus) {
            if (menu.getPath().equals(url)) {
                return true;
            }
        }
        return false;
    }
}
